import {
    UserOutlined,
    PropertySafetyOutlined,
    CreditCardOutlined,
    FundViewOutlined,
    MenuUnfoldOutlined,
    LineChartOutlined,
    AlignLeftOutlined
} from '@ant-design/icons';
import type { MenuProps } from 'antd';

export type MenuItem = Required<MenuProps>['items'][number];
export function getItem(
    label: React.ReactNode,
    key: React.Key,
    icon?: React.ReactNode,
    children?: MenuItem[],
    type?: 'group',
): MenuItem {
    return {
        key,
        icon,
        children,
        label,
        type,
    } as MenuItem;
}
//菜单路由映射Map
export const PathRouteMap: DataType[] = [
    {
        key: "1",
        type: "multiple",
        name: "客户管理",
        children: [
            {
                key: '11',
                type: "single",
                RoutePath: "/home/customer",
                name: "客户管理"
            },
            {
                key: '12',
                type: "single",
                RoutePath: "/home/loss",
                name: "流失客户"
            }
        ]
    },
    {
        key: "2",
        type: "multiple",
        name: "产品管理",
        children: [
            {
                key: '21',
                type: "single",
                RoutePath: "/home/product",
                name: "产品列表"
            },
            {
                key: '22',
                type: "single",
                RoutePath: "/home/prodType",
                name: "产品类型"
            }
        ]
    },
    {
        key: "sub",
        type: "multiple",
        name: "订单管理",
        children: [
            {
                key: "31",
                RoutePath: "/home/order",
                type: "single",
                name: "订单管理"
            },
            {
                key: "32",
                RoutePath: "/home/myOrder",
                type: "single",
                name: "我的订单"
            },
        ]
    },
    {
        key: "4",
        type: "multiple",
        name: "活动管理",
        children: [{
            key: "41",
            RoutePath: "/home/active",
            type: "single",
            name: "活动广场"
        },{
            key: "42",
            RoutePath: "/home/myActive",
            type: "single",
            name: "我的活动"
        }]
    },
    {
        key: "5",
        type: "multiple",
        name: "服务管理",
        children: [
            {
            key: "51",
            RoutePath: "/home/server",
            type: "single",
            name: "服务管理"
           },
           {
            key: "52",
            RoutePath: "/home/myServe",
            type: "single",
            name: "我的服务"
           },

    ]
    },
    {
        key: "6",
        type: "multiple",
        name: "市场报表",
        children: [
            {
            key: "61",
            RoutePath: "/home/RForm1",
            type: "single",
            name: "活动报表"
           },
           {
            key: "62",
            RoutePath: "/home/RForm2",
            type: "single",
            name: "产品销量报表"
           },
           {
            key: "63",
            RoutePath: "/home/RForm3",
            type: "single",
            name: "流失客户报表"
           },
    ]
    }
]


export const items = (account:any)=>{
    return  [
        account == 'admin'&&getItem('客户系统', '1', <UserOutlined />, [
            getItem('客户管理', '11'),
            getItem('流失客户', '12'),
        ]),
        getItem('产品管理', '2', <PropertySafetyOutlined />, [
            getItem('产品列表', '21'),
            account == 'admin'&& getItem('产品类型', '22') as any,
        ]),
        getItem('订单管理', 'sub', <CreditCardOutlined />, [
            account == 'admin'&& getItem('订单管理', '31') as any,
            getItem('我的订单', '32'),
        ]),
        getItem('活动管理', 'sub2', <AlignLeftOutlined />, [
            getItem('活动广场', '41'),
            getItem('我的活动', '42'),
        ]),
        getItem('服务管理', '5', <FundViewOutlined />, [
            account == 'admin'&& getItem('服务管理', '51') as any,
            getItem('我的服务', '52'),
        ]),
        account == 'admin'&&getItem('市场报表', '6', <LineChartOutlined />, [
            getItem('活动报表', '61'),
            getItem('产品销量报表', '62'),
            getItem('流失客户报表', '63'),
        ])
    ] as MenuItem[] 
    
}

// 查找items中对应的Item
export function findItem(data: any, key: string) {
    let result = null
    if (data.children) {
        for (let i = 0; i < data.children.length; i++) {
            if (data.children[i].key == key) {
                result = data.children[i]
                break;
            }
            if (data.children[i].children) {
                let res: any = findItem(data.children[i], key)
                if (res != null) {
                    result = res
                }
            }
        }
    } else {
        result = data
    }
    return result
}


interface DataType {
    key: string,
    RoutePath?: string,
    type: string,
    name: string,
    children?: DataType[]
}

// 查找RouteMap 中对应的Route key
export function findRoute(key: string, data: DataType[] = PathRouteMap): string {
    let Path;

    for (let i = 0; i < data.length; i++) {
        if (data[i].type == "single" && data[i].key == key) {
            Path = data[i].RoutePath as string
            break;
        }

        if (data[i].type == "multiple") {
            let path = findRoute(key, data[i].children as DataType[])
            if (path) {
                Path = path
                break;
            }
        }
    }

    return Path as string
}